<template>
  <el-dialog
    :title="!dataForm.recordId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="记录户型" prop="recordType">
      <el-input v-model="dataForm.recordType" placeholder="记录户型"></el-input>
    </el-form-item>
    <el-form-item label="所属小区" prop="recordName">
      <el-input v-model="dataForm.recordName" placeholder="所属小区"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="recordArea">
      <el-input v-model="dataForm.recordArea" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="recordPrice">
      <el-input v-model="dataForm.recordPrice" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="成交时间" prop="recordGtime">
      <el-input v-model="dataForm.recordGtime" placeholder="成交时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          recordId: 0,
          recordType: '',
          recordName: '',
          recordArea: '',
          recordPrice: '',
          recordGtime: ''
        },
        dataRule: {
          recordType: [
            { required: true, message: '记录户型不能为空', trigger: 'blur' }
          ],
          recordName: [
            { required: true, message: '所属小区不能为空', trigger: 'blur' }
          ],
          recordArea: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          recordPrice: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          recordGtime: [
            { required: true, message: '成交时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.recordId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.recordId) {
            this.$http({
              url: this.$http.adornUrl(`/twohouse/record/info/${this.dataForm.recordId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.recordType = data.record.recordType
                this.dataForm.recordName = data.record.recordName
                this.dataForm.recordArea = data.record.recordArea
                this.dataForm.recordPrice = data.record.recordPrice
                this.dataForm.recordGtime = data.record.recordGtime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/twohouse/record/${!this.dataForm.recordId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'recordId': this.dataForm.recordId || undefined,
                'recordType': this.dataForm.recordType,
                'recordName': this.dataForm.recordName,
                'recordArea': this.dataForm.recordArea,
                'recordPrice': this.dataForm.recordPrice,
                'recordGtime': this.dataForm.recordGtime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
